package com.xm.dao.reception_desk;

import com.xm.exception.SqlErrorException;
import com.xm.util.BaseUtil;
import org.intellij.lang.annotations.Language;

import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * 商品服务表
 * @author 康文程
 * @date 2024-1-7 下午 07:54
 */
public class CommodityServeDao extends BaseUtil {
    /**
     * 根据商品ID查询是否为自营商品
     *
     * @param commodityId 商品ID
     * @return 是否为自营商品，true代表是，false代表不是
     * @throws SqlErrorException 抛出SQL错误
     */
    public boolean isSelfEmployedByCommodityId(Integer commodityId) throws SqlErrorException {
        @Language("sql")
        String sql = "SELECT is_self_employed FROM commodity_serve WHERE commodity_id = ?";
        ResultSet resultSet = executeQuery(sql, commodityId);

        try {
            if (resultSet.next()) {
                int isSelfEmployed = resultSet.getInt("is_self_employed");
                return isSelfEmployed == 0;
            }
        } catch (SQLException e) {
            throw new SqlErrorException("查询失败", e);
        } finally {
            close(resultSet);
        }

        return false;
    }
}
